<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class AutoMarkingRecord extends Model
{
    use HasFactory, SoftDeletes;

    protected $table = 'auto_marking_records';

    protected $fillable = [
        'subjective_id',
        'subjective_item_id',
        'group_id',
        'question_no',
        'secret_num',
        'image_url',
        'question_data',
        'ocr_content_list',
        'ocr_full_text',
        'word_count',
        'line_count',
        'ai_score_results',
        'average_score',
        'adjusted_score',
        'submitted_score',
        'submit_result',
        'is_submitted',
        'error_message',
        'processed_at',
    ];

    protected $casts = [
        'question_data' => 'array',
        'ocr_content_list' => 'array',
        'ai_score_results' => 'array',
        'submit_result' => 'array',
        'average_score' => 'decimal:2',
        'adjusted_score' => 'decimal:2',
        'submitted_score' => 'decimal:2',
        'is_submitted' => 'boolean',
        'processed_at' => 'datetime',
    ];

    protected $dates = [
        'processed_at',
        'created_at',
        'updated_at',
        'deleted_at',
    ];

    /**
     * 获取格式化的OCR内容
     */
    public function getFormattedOcrContentAttribute()
    {
        if (!$this->ocr_content_list) {
            return '';
        }
        
        return implode("\n", $this->ocr_content_list);
    }

    /**
     * 获取AI评分详情摘要
     */
    public function getAiScoreSummaryAttribute()
    {
        if (!$this->ai_score_results) {
            return [];
        }

        $summary = [];
        foreach ($this->ai_score_results as $model => $result) {
            if (isset($result['res']['总分'])) {
                $summary[$model] = $result['res']['总分'];
            }
        }
        
        return $summary;
    }

    /**
     * 获取处理状态
     */
    public function getStatusAttribute()
    {
        if ($this->error_message) {
            return 'error';
        }
        
        if ($this->is_submitted) {
            return 'submitted';
        }
        
        if ($this->processed_at) {
            return 'processed';
        }
        
        return 'pending';
    }

    /**
     * 获取状态文本
     */
    public function getStatusTextAttribute()
    {
        switch ($this->status) {
            case 'error':
                return '处理失败';
            case 'submitted':
                return '已提交';
            case 'processed':
                return '已处理';
            case 'pending':
            default:
                return '待处理';
        }
    }

    /**
     * 作用域：已提交的记录
     */
    public function scopeSubmitted($query)
    {
        return $query->where('is_submitted', true);
    }

    /**
     * 作用域：处理失败的记录
     */
    public function scopeWithErrors($query)
    {
        return $query->whereNotNull('error_message');
    }

    /**
     * 作用域：按处理时间排序
     */
    public function scopeOrderByProcessed($query, $direction = 'desc')
    {
        return $query->orderBy('processed_at', $direction);
    }
}